home *** CD-ROM | disk | FTP | other *** search
- //---------------------------cut-here--8<-------------------------------------
- //
- //
- // xgetch.mh
- // eXtended GETCH() and extended input() (xinput())
- // by Golden Spud
- //
- // handles ANSI/WordStar cursor keys from remote as well as local
- //
- // known bugs:
- // wont accept all keys.
- // INS (^[On) needs to be added to ANSI
- //
-
- #ifndef __XGETCH_MH
- #define __XGETCH_MH
-
- #define X_UP 72
- #define X_DOWN 80
- #define X_LEFT 75
- #define X_RIGHT 77
- #define X_HOME 71
- #define X_END 79
- #define X_INSERT 82
- #define X_DELETE 83
- #define X_PAGEUP 73
- #define X_PAGEDOWN 81
-
- #define X_INVALID 255
- #define X_EMPTY 0
-
- // this is the number of tries that xgetch() will wait for the rest of
- // an ANSI escape sequence before assuming that the escape key was pressed
- // instead of an arrow key.
- #define XGETCH_ANSI_TRIES 500
-
- char: _xgetchinput;
-
- char xgetch()
- {
- char: tempch;
-
- // if there's a character waiting, let's return it without wasting
- // any time.
- if (_xgetchinput <> X_EMPTY) {
- tempch := _xgetchinput;
- _xgetchinput := X_EMPTY;
- return tempch;
- }
-
- tempch := getch();
-
- // is it an extended character? if so, store it and return 0.
- if (tempch = 0) {
- _xgetchinput := getch();
- return tempch;
- }
-
- // -------------------- WORDSTAR DIAMOND CONTROLS ------------------
- if (tempch = 23) { // ctrl-w = home
- _xgetchinput := X_HOME;
- return 0;
- }
- if (tempch = 16) { // ctrl-p = end
- _xgetchinput := X_END;
- return 0;
- }
- if (tempch = 18) { // ctrl-r = pgup
- _xgetchinput := X_PAGEUP;
- return 0;
- }
- if (tempch = 3) { // ctrl-c = pgdn
- _xgetchinput := X_PAGEDOWN;
- return 0;
- }
- if (tempch = 5) { // ctrl-e = up
- _xgetchinput := X_UP;
- return 0;
- }
- if (tempch = 24) { // ctrl-x = down
- _xgetchinput := X_DOWN;
- return 0;
- }
- if (tempch = 19) { // ctrl-s = left
- _xgetchinput := X_LEFT;
- return 0;
- }
- if (tempch = 4) { // ctrl-d = right
- _xgetchinput := X_RIGHT;
- return 0;
- }
- if (tempch = 22) { // ctrl-v = insert
- _xgetchinput := X_INSERT;
- return 0;
- }
- if (tempch = 7) { // ctrl-g = del
- _xgetchinput := X_DELETE;
- return 0;
- }
-
- // -------------------- ANSI ESCAPE CONTROLS ------------------------
- if (tempch = 27) { // escape means it's an ANSI code
- int: tries;
- char: tempch2;
-
- // here we need to test to see if the [ character comes through.
- tries := 0;
- while (tries < XGETCH_ANSI_TRIES)
- if (kbhit() = FALSE)
- tries := tries + 1;
- else
- goto xgetch_breakout1;
- xgetch_breakout1:
-
- // if no key has been hit yet, assume the user actually pressed
- // ESC and return that.
- if (kbhit() = FALSE)
- return 27;
-
- tempch2 := getch();
-
- // if the input character was not '[', then assume the escape key
- // was hit, then some other character.
- if (tempch2 <> '[') {
- _xgetchinput := tempch2;
- return 27;
- }
-
- // now we need to test for the next character. if it isn't in the
- // list of valid characters, we will return 0 with X_INVALID as the
- // extended
- // key, meaning an invalid key.
- // if it doesn't come in, we'll assume the user pressed escape, then
- // [ and we'll return both of those.
- tries := 0;
- while (tries < XGETCH_ANSI_TRIES)
- if (kbhit() = FALSE)
- tries := tries + 1;
- else
- goto xgetch_breakout2;
- xgetch_breakout2:
-
- if (kbhit() = FALSE) {
- _xgetchinput := '[';
- return 27;
- }
-
- tempch2 := getch();
-
- if (tempch2 = 'H') { // home
- _xgetchinput := X_HOME;
- return 0;
- }
- if (tempch2 = 'K') { // end
- _xgetchinput := X_END;
- return 0;
- }
- if (tempch2 = 'A') { // up
- _xgetchinput := X_UP;
- return 0;
- }
- if (tempch2 = 'B') { // down
- _xgetchinput := X_DOWN;
- return 0;
- }
- if (tempch2 = 'D') { // left
- _xgetchinput := X_LEFT;
- return 0;
- }
- if (tempch2 = 'C') { // right
- _xgetchinput := X_RIGHT;
- return 0;
- }
- if (tempch2 = 'O') { // pgup or pgdn -- further testing
- char: tempch3;
-
- tries := 0;
- while (tries < XGETCH_ANSI_TRIES)
- if (kbhit() = FALSE)
- tries := tries + 1;
- else
- goto xgetch_breakout3;
- xgetch_breakout3:
-
- if (kbhit() = FALSE) {
- _xgetchinput := X_INVALID;
- return 0;
- }
-
- if (tempch3 = 'r') { // pgup
- _xgetchinput := X_PAGEUP;
- return 0;
- }
- if (tempch3 = 'q') { // pgdn
- _xgetchinput := X_PAGEDOWN;
- return 0;
- }
- // invalid ^[[O sequence
- _xgetchinput := X_INVALID;
- return 0;
- }
-
- // invalid ^[[ sequence
- _xgetchinput := X_INVALID;
- return 0;
- }
-
- // it was a normal key and should be returned as such.
- return tempch;
- }
-
- #endif
-
-
-